import { useUserInfo } from '@/stores/userinfo'
export const permission = {
  mounted(el: HTMLElement, binding: { value: string | string[] }) {
    const { scope } = useUserInfo()
    // 将 binding.value 转换为数组，支持传入单个字符串或字符串数组
    const permissions = Array.isArray(binding.value) ? binding.value : [binding.value]
    // 检查用户是否有权限
    const hasPermission = permissions.some(permission => scope.includes(permission))
    // 如果没有权限，移除元素
    if (!hasPermission) {
      el.parentNode?.removeChild(el)
    }
  },
}

export const role = (el: HTMLElement, binding: { value: string }) => {
  const { roles, user } = useUserInfo()
  console.log(el, binding.value, roles, user)

  
    if(!binding.value.includes(binding.value)){
      el.style.display = 'none'
    }
  
}

export const authorization = {
  install(app: Record<'directive',(p:string,p2:unknown)=>void>) {
    app.directive('permission', permission)
    app.directive('role', role)
  },
}
